<template>
  <a-modal
    :open="open"
    width="1200px"
    :title="rowData?.lsh ? '修改防疫登记' : '新增防疫登记'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <div class="flex">
      <div class="w-1/2">
        <BaseTitle title="基础信息" />
        <div class="h-[600px] overflow-auto">
          <PatientInfo
            v-model:value="formState"
            :patient-type="'入院登记'"
            :show-divider-bot-info="false"
            :table-width="700"
            item-class="w-1/2"
            :search-params="{
              pyjm: formState.brName,
              yyzt: '0'
            }"
          />
        </div>
      </div>
      <div class="w-1/2">
        <BaseTitle title="防疫信息" />
        <a-form class="form-style-flex" name="validate_other" :model="formState" :label-col="{ style: { width: '130px' } }">
          <a-form-item label="防疫风险" class="w-1/2">
            <a-input v-model:value="formState.epidemicDesc" placeholder="请输入" />
          </a-form-item>
          <a-form-item label="登记日期" class="w-1/2" v-bind="validateInfos.registrationDate">
            <a-date-picker v-model:value="formState.registrationDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
          </a-form-item>
          <a-form-item label="本舰人员" class="w-1/2">
            <a-radio-group v-model:value="formState.isLocal">
              <a-radio :value="0">否</a-radio>
              <a-radio :value="1">是</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="是否在舰" class="w-1/2">
            <a-radio-group v-model:value="formState.isOnShip">
              <a-radio :value="0">否</a-radio>
              <a-radio :value="1">是</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="登舰目的" class="w-full" v-bind="validateInfos.purposeDesc">
            <a-input v-model:value="formState.purposeDesc" placeholder="请输入" />
          </a-form-item>
          <a-form-item label="是否14天高风险" class="w-1/2">
            <a-radio-group v-model:value="formState.isHighRisk">
              <a-radio :value="0">否</a-radio>
              <a-radio :value="1">是</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="阳性既往史" class="w-1/2">
            <a-radio-group v-model:value="formState.isPositiveHistory">
              <a-radio :value="0">否</a-radio>
              <a-radio :value="1">是</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="有无其他异常情况" class="w-1/2">
            <a-radio-group v-model:value="formState.isOtherUnusual">
              <a-radio :value="0">否</a-radio>
              <a-radio :value="1">是</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="信息收集人" class="w-1/2">
            <SelectTable
              v-model:value="formState.empName"
              type="医生"
              @select="
                (select) => {
                  formState.empName = select?.empName
                  formState.empId = select?.empId
                }
              "
            />
          </a-form-item>
          <a-form-item label="备注" class="w-full mb-3">
            <a-textarea v-model:value="formState.memo" :rows="6" placeholder="请输入" />
          </a-form-item>
        </a-form>
      </div>
    </div>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { post } from '@/utils/request'
import PatientInfo from '@/components/LsolationRegistration/PatientInfo'

const emit = defineEmits(['update:open', 'update:value', 'success'])

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const formState = ref({
  registrationDate: dayjs().format('YYYY-MM-DD'),
  purposeDesc: ''
})
const rulesState = ref({
  registrationDate: [{ required: true, message: '请选择登记日期' }],
  purposeDesc: [{ required: true, message: '请输入目的' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  if (!formState.value.brName || !Number(formState.value.brAge)) {
    message.error('请填写患者信息')
    return
  }
  // 保存患者信息
  const res = await post('/common/patient/savePatientInfo', { ...formState.value })
  if (res.code !== 0) return message.error(res.msg)
  const { code, msg } = await post('/war/epidemicPrevention/doSaveEpidemicRegistration', { ...formState.value })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('success')
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}

// 获取患者信息
const getPatientInfo = async () => {
  const { code, data = {} } = await get('/common/patient/getPatientInfo?br_id=' + props.rowData.brId)
  if (code !== 0) return message.error('获取患者信息失败')
  formState.value = { ...data, ...props.rowData }
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData.brId) {
        getPatientInfo()
      } else {
        formState.value = { isLocal: 0, isOnShip: 0, isHighRisk: 0, isPositiveHistory: 0, isOtherUnusual: 0 }
      }
    }
  }
)
</script>
<style lang="less" scoped></style>
